package cn.zlp.pay.service.impl;

import cn.zlp.basic.exception.BusinessException;
import cn.zlp.basic.service.impl.BaseServiceImpl;
import cn.zlp.pay.domain.AlipayInfo;
import cn.zlp.pay.domain.PayBill;
import cn.zlp.pay.mapper.AlipayInfoMapper;
import cn.zlp.pay.mapper.PayBillMapper;
import cn.zlp.pay.service.IPayBillService;
import cn.zlp.pay.utils.AlipayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class PayBillServiceImpl extends BaseServiceImpl<PayBill> implements IPayBillService {

    @Autowired(required = false)
    private AlipayInfoMapper alipayInfoMapper;

    @Autowired(required = false)
    private PayBillMapper payBillMapper;


    @Override
    public String payRequest(PayBill payBillTem) {
        if (payBillTem == null) {
            throw new BusinessException("支付单不存在,请重新创建");
        }
        PayBill payBill = payBillMapper.loadByUnionPaySn(payBillTem.getUnionPaySn());
        if (payBill == null) {
            throw new BusinessException("支付单不存在,请重新创建");
        }
        //支付宝信息
        AlipayInfo alipayInfo = alipayInfoMapper.loadByShopId(payBill.getShop_id());
        switch(payBill.getPayChannel().intValue()){
            case 1 :
                String pay = AlipayUtils.pay(alipayInfo, payBill);
                return pay;
            //微信
            case 2 :
                //@TODO
                System.out.println("微信");
                break;
            //银联
            case 3 :
                //@TODO
                System.out.println("银联");
                break;
            //余额:0
            default :
                System.out.println("余额");
                //@TODO
        }
        return null;
    }

    /**
     * 根据支付单唯一标识查询支付单
     * @param unionPaySn
     * @return
     */
    @Override
    public PayBill loadByUnionPaySn(String unionPaySn) {
        return payBillMapper.loadByUnionPaySn(unionPaySn);
    }
}
